﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static WinFormsApp1.Leo.ClientserShow;

namespace WinFormsApp1.Leo.week02
{
    //待宰圈管理列表
    public partial class SlaughtrerdShow : Form
    {
        public SlaughtrerdShow()
        {
            InitializeComponent();
            GetWaiting();
        }

        private record Supplier();



        //待宰圈号
        public async void GetWaiting()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<Waitingss>>>(URLCommon.Url2, "/WeekListOne_Leo/GeWaitingShow", HttpClientHelper.MethodType.GET, default);

            comboBox2.DataSource = items.Data;
            comboBox2.DisplayMember = "WaitingName";
            comboBox2.ValueMember = "WaitingId";
        }

        public class Waitingss
        {
            public int WaitingId { get; set; }
            public string WaitingName { get; set; }
        }

        public class Slaughteredse
        {
            public int pageIndex { get; set; }

            public int pageSize { get; set; }

            public int WaitingId { get; set; }
        };

        public int pageIndex = 1;

        public int pageSize = 3;

        public int totalcount = 0;

        public int pagecount = 0;

        public int WaitingId = 0;

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void SlaughtrerdShow_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;

            var clients = new Slaughteredse
            {
                pageIndex = pageIndex,
                pageSize = pageSize,
                WaitingId = WaitingId
            };

            var items = await HttpClientHelper.RequestUrl<object, ApiResultFY1<List<Slaughtrerdse>>>(URLCommon.Url2, "/WeekListOne_Leo/GetSlaughteredShow", HttpClientHelper.MethodType.POST, clients);
            this.dataGridView1.DataSource = items.Data.list;
            totalcount = items.Data.totalcount;
            pagecount = items.Data.pagecount;
        }

        public class Slaughtrerdse
        {
            public int SlaughteredId { get; set; }

            /// <summary>
            /// 商品代号(外键)
            /// </summary>
            public string UPCDataName { get; set; }

            /// <summary>
            /// 净重
            /// </summary>
            public string LiveAnimalsWeight { get; set; }

            /// <summary>
            /// 均重
            /// </summary>
            public string LiveAnimalsRAverageWeight { get; set; }

            /// <summary>
            /// 进场登记编号
            /// </summary>
            public string CheckInNo { get; set; }

            /// <summary>
            /// 进场数量
            /// </summary>
            public int ApproachNum { get; set; }

            /// <summary>
            /// 待宰圈号（外键）
            /// </summary>
            public string WaitingName { get; set; }

            /// <summary>
            /// 入圈时间
            /// </summary>
            public DateTime? SlaughteredTime { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            public string SlaughteredRemark { get; set; }

        }

        private void dataGridView1_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dataGridView1_CellPainting_1(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "操作")
                {
                    StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;//设置重绘单元格样式
                    sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming = StringTrimming.EllipsisCharacter;

                    e.PaintBackground(e.CellBounds, false);//重绘边框

                    //设置写入字体大小
                    System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    SizeF sizeDel = e.Graphics.MeasureString("清圈", myFont);
                    SizeF sizeMod = e.Graphics.MeasureString("移圈", myFont);

                    float FDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);
                    float FMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                    //设置每个“按钮的边界”
                    RectangleF recDel = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * FDel, e.CellBounds.Height);
                    RectangleF recMod = new RectangleF(recDel.Right, e.CellBounds.Top, e.CellBounds.Width * FMod, e.CellBounds.Height);
                    e.Graphics.DrawString("清圈", myFont, Brushes.Black, recDel, sf);
                    e.Graphics.DrawString("移圈", myFont, Brushes.Black, recMod, sf);
                    e.Handled = true;

                }
            }
        }

        public record SlaughteredDel(int SlaughteredId);

        private async void dataGridView1_CellMouseClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                Point curPosition = e.Location;
                if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "操作")
                {
                    Graphics g = this.dataGridView1.CreateGraphics();
                    System.Drawing.Font myFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    SizeF sizeDel = g.MeasureString("清圈", myFont);
                    SizeF sizeMod = g.MeasureString("移圈", myFont);
                    float FDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);
                    float FMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                    Rectangle rectTotal = new Rectangle(0, 0, this.dataGridView1.Columns[e.ColumnIndex].Width, this.dataGridView1.Rows[e.RowIndex].Height);
                    RectangleF rectDel = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * FDel, rectTotal.Height);
                    RectangleF rectMod = new RectangleF(rectDel.Right, rectTotal.Top, rectTotal.Width * FMod, rectTotal.Height);

                    if (rectDel.Contains(curPosition))
                    {
                        if (dataGridView1.Columns[e.ColumnIndex].Name == "Clas" && e.RowIndex >= 0)
                        {
                            if (MessageBox.Show("您确定要删除吗？", "此删除为逻辑删除", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
                            {
                                int SlaughteredId = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["序列"].Value);

                                SlaughteredDel slaughteredDel = new(SlaughteredId);

                                var items = await HttpClientHelper.RequestUrl<SlaughteredDel, ApiResult<List<SlaughteredDeler>>>(URLCommon.Url1, "/WorkList01_leo/GetSlaugDel", HttpClientHelper.MethodType.POST, slaughteredDel);

                                if (items.Code == 1)
                                {
                                    MessageBox.Show("删除成功");
                                }
                                else
                                {
                                    MessageBox.Show("删除失败");
                                }
                            }

                        }

                    }
                    else
                    if (rectMod.Contains(curPosition))
                    {

                        if (e.RowIndex >= 0)
                        {
                            int SlaughteredId = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["序列"].Value);

                            SlaughteredUpdate slaughteredUpdate = new(SlaughteredId);
                            slaughteredUpdate.Show();
                        }

                    }

                }
            }
        }

        public class SlaughteredDeler
        {
            public int SlaughteredId { get; set; }
        }

        /// <summary>
        /// 入圈
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            SlaughteredAdd slaughteredAdd = new();
            slaughteredAdd.Show();
            this.Hide();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (comboBox2.Text != null)
            {
                WaitingId = Convert.ToInt32(comboBox2.SelectedValue);
            }
            SlaughtrerdShow_Load(sender, e);
        }

        //首页
        private void button1_Click(object sender, EventArgs e)
        {
            if (pageIndex != 1)
            {
                pageIndex = 1;
            }
            SlaughtrerdShow_Load(sender, e);
        }

        //上一页
        private void button2_Click(object sender, EventArgs e)
        {
            if (pageIndex > 1)
            {
                pageIndex--;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经第一页了");
            }
            SlaughtrerdShow_Load(sender, e);
        }

        //下一页
        private void button3_Click(object sender, EventArgs e)
        {
            if (pageIndex < pagecount)
            {
                pageIndex++;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经最后一页了");
            }
            SlaughtrerdShow_Load(sender, e);
        }

        //尾页
        private void button4_Click(object sender, EventArgs e)
        {
            if (pageIndex != pagecount)
            {
                pageIndex = pagecount;
            }
            SlaughtrerdShow_Load(sender, e);
        }

        
    }
}
